package com.blacktech.dbu.meta.controller;

import com.blacktech.dbu.meta.dto.CreateColumnFromLibraryRequest;
import com.blacktech.dbu.meta.entity.DbuColumn;
import com.blacktech.dbu.meta.service.ColumnService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 表字段管理控制器
 * 支持从字段库选择字段和自定义字段
 *
 * @author Yanyunsong
 */
@RestController
@RequestMapping("/api/metadata/columns")
@Tag(name = "表字段管理", description = "表字段管理相关接口，包含从字段库创建等高级业务逻辑")
public class ColumnController {

    @Autowired
    private ColumnService columnService;

    @PostMapping("/from-library")
    @Operation(summary = "从字段库创建字段", description = "从字段库中选择字段创建表字段")
    public DbuColumn createColumnFromLibrary(@Valid @RequestBody CreateColumnFromLibraryRequest request) {
        return columnService.createColumnFromLibrary(request);
    }

    @PostMapping("/custom")
    @Operation(summary = "创建自定义字段", description = "创建不来自字段库的自定义字段")
    public DbuColumn createCustomColumn(@Valid @RequestBody DbuColumn column) {
        return columnService.createCustomColumn(column);
    }

    @PostMapping("/batch-from-library")
    @Operation(summary = "批量从字段库创建字段", description = "批量从字段库中选择字段创建表字段")
    public List<DbuColumn> batchCreateColumnsFromLibrary(
            @Parameter(description = "表ID", required = true) @RequestParam Long tableId,
            @Parameter(description = "字段库ID列表", required = true) @RequestBody List<Long> fieldLibraryIds) {

        return columnService.batchCreateColumnsFromLibrary(tableId, fieldLibraryIds);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新字段", description = "更新字段信息")
    public DbuColumn updateColumn(
            @Parameter(description = "字段ID") @PathVariable Long id,
            @Valid @RequestBody DbuColumn column) {

        return columnService.updateColumn(id, column);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除字段", description = "删除指定字段")
    public void deleteColumn(@Parameter(description = "字段ID") @PathVariable Long id) {
        columnService.deleteColumn(id);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取字段详情", description = "根据ID获取字段详情")
    public DbuColumn getColumn(@Parameter(description = "字段ID") @PathVariable Long id) {
        return columnService.getColumnById(id);
    }

    @GetMapping("/table/{tableId}")
    @Operation(summary = "获取表的字段列表", description = "根据表ID获取所有字段")
    public List<DbuColumn> getColumnsByTableId(@Parameter(description = "表ID") @PathVariable Long tableId) {
        return columnService.getColumnsByTableId(tableId);
    }

    @GetMapping("/check-exists")
    @Operation(summary = "检查字段名是否存在", description = "检查指定表中是否存在某个字段名")
    public Boolean checkColumnNameExists(
            @Parameter(description = "表ID", required = true) @RequestParam Long tableId,
            @Parameter(description = "字段名", required = true) @RequestParam String columnName) {

        return columnService.existsColumnNameInTable(tableId, columnName);
    }
}